package com.fs.sys.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.sys.bean.dto.DeptAppDTO;
import com.fs.sys.bean.query.DeptAppQuery;
import com.fs.sys.bean.req.DeptAppAddReq;
import com.fs.sys.entityservice.DeptAppEntityService;
import com.fs.common.annotations.Log;
import com.fs.common.bean.LogType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织应用关系控制器 
 * @author Coder Generator 2025-06-06 11:03:10 
 **/
@RestController
@RequestMapping("/sys/dept-app")
@Slf4j
@Tag(name = "组织应用关系控制器")
public class DeptAppController {
    @Autowired
    private DeptAppEntityService service;

    @PostMapping("add")
    @Operation(summary = "新增")
    @Log(value = "机构应用", op = LogType.NEW)
    public void add(@RequestBody @Validated DeptAppDTO req) {
        service.insert(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    @Log(value = "机构应用", op = LogType.EDIT)
    public void update(@RequestBody @Validated DeptAppDTO req) {
        service.update(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @Log(value = "机构应用", op = LogType.EDIT)
    public DeptAppDTO save(@RequestBody @Validated DeptAppDTO req) {
        if (StringUtils.isBlank(req.getId())) {
            return service.insert(req);
        }
        service.update(req);
        return req;
    }

    @PostMapping("update-dept-app")
    @Log(value = "机构应用", op = LogType.EDIT)
    public void saveDeptApp(@RequestBody DeptAppAddReq req) {
        service.deleteBuilder()
                .eq("app_id", req.getAppId())
                .delete();
        List<DeptAppDTO> list = new ArrayList<>(16);
        req.getDeptIds().stream().distinct().forEach(deptId -> {
            DeptAppDTO deptAppDTO = new DeptAppDTO();
            list.add(deptAppDTO);
            deptAppDTO.setDeptId(deptId);
            deptAppDTO.setAppId(req.getAppId());
        });

        if (!CollectionUtils.isEmpty(list)) {
            service.insert(list);
        }
    }

    @PostMapping("remove")
    @Log(value = "机构应用", op = LogType.DELETE)
    public void removeDeptApp(@RequestBody DeptAppAddReq req) {
        service.deleteBuilder()
                .in("dept_id", req.getDeptIds())
                .eq("app_id", req.getAppId())
                .delete();
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    @Log(value = "机构应用", op = LogType.DELETE)
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public DeptAppDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<DeptAppDTO> pageQuery(@RequestBody DeptAppQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<DeptAppDTO> query(@RequestBody DeptAppQuery query) {
        return service.query(query);
    }
}